package top.itmeans.yes.portal.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import top.itmeans.yes.entity.CartItem;
import top.itmeans.yes.entity.Member;
import top.itmeans.yes.mapper.CartItemMapper;
import top.itmeans.yes.portal.domain.CartProduct;
import top.itmeans.yes.portal.domain.CartPromotionItem;
import top.itmeans.yes.portal.mapper.PortalProductMapper;
import top.itmeans.yes.portal.service.CartItemService;
import top.itmeans.yes.portal.service.MemberService;
import top.itmeans.yes.portal.service.PromotionService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 购物车表 服务实现类
 * </p>
 *
 * @author Yiming.Cao
 * @since 2022-04-28
 */
@Service
public class CartItemServiceImpl extends ServiceImpl<CartItemMapper, CartItem> implements CartItemService {
    @Autowired
    private CartItemMapper cartItemMapper;
    @Autowired
    private PortalProductMapper portalProductMapper;
    @Autowired
    private PromotionService promotionService;
    @Autowired
    private MemberService memberService;

    @Override
    public int add(CartItem cartItem) {
        int count;
        Member currentMember =memberService.getCurrentMember();
        cartItem.setMemberId(currentMember.getId());
        cartItem.setMemberNickname(currentMember.getNickname());
        cartItem.setDeleteStatus(0);
        CartItem existCartItem = getCartItem(cartItem);
        if (existCartItem == null) {
            cartItem.setCreateDate(LocalDateTime.now());
            count = cartItemMapper.insert(cartItem);
        } else {
            cartItem.setModifyDate(LocalDateTime.now());
            existCartItem.setQuantity(existCartItem.getQuantity() + cartItem.getQuantity());
            count = cartItemMapper.updateById(existCartItem);
        }
        return count;
    }

    /**
     * 根据会员id,商品id和规格获取购物车中商品
     */
    private CartItem getCartItem(CartItem cartItem) {
        LambdaQueryWrapper<CartItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CartItem::getMemberId,cartItem.getMemberId())
                .eq(CartItem::getProductId,cartItem.getProductId());
        List<CartItem> cartItemList = cartItemMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(cartItemList)) {
            return cartItemList.get(0);
        }
        return null;
    }

    @Override
    public List<CartItem> list(Long memberId) {
        LambdaQueryWrapper<CartItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CartItem::getMemberId,memberId);
        return cartItemMapper.selectList(queryWrapper);
    }

    @Override
    public List<CartPromotionItem> listPromotion(Long memberId, List<Long> cartIds) {
        List<CartItem> cartItemList = list(memberId);
        if(CollUtil.isNotEmpty(cartIds)){
            cartItemList = cartItemList.stream().filter(item->cartIds.contains(item.getId())).collect(Collectors.toList());
        }
        List<CartPromotionItem> cartPromotionItemList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(cartItemList)){
            cartPromotionItemList = promotionService.calcCartPromotion(cartItemList);
        }
        return cartPromotionItemList;
    }

    @Override
    public int updateQuantity(Long id, Long memberId, Integer quantity) {
        CartItem cartItem = new CartItem();
        cartItem.setQuantity(quantity);
        LambdaUpdateWrapper<CartItem> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CartItem::getId,id).eq(CartItem::getMemberId,memberId);
        return cartItemMapper.update(cartItem, updateWrapper);
    }

    @Override
    public int delete(Long memberId, List<Long> ids) {
        LambdaUpdateWrapper<CartItem> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(CartItem::getMemberId,memberId);
        queryWrapper.in(CartItem::getId,ids);
        return cartItemMapper.delete(queryWrapper);
    }

    @Override
    public CartProduct getCartProduct(Long productId) {
        return portalProductMapper.getCartProduct(productId);
    }

    @Override
    public int updateAttr(CartItem cartItem) {
        //删除原购物车信息
        CartItem updateCart = new CartItem();
        updateCart.setId(cartItem.getId());
        updateCart.setModifyDate(LocalDateTime.now());
        updateCart.setDeleteStatus(1);
        cartItemMapper.updateById(updateCart);
        cartItem.setId(null);
        add(cartItem);
        return 1;
    }

    @Override
    public int clear(Long memberId) {
        LambdaUpdateWrapper<CartItem> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(CartItem::getMemberId,memberId);
        return cartItemMapper.delete(queryWrapper);
    }

}
